package com.qy.service.impl;

import com.qy.dao.*;
import com.qy.model.Comment;
import com.qy.model.Member;
import com.qy.model.Policy;
import com.qy.model.PolicyOrder;
import com.qy.service.PolicyOrderService;
import com.qy.base.core.AbstractService;
import com.qy.service.PolicyService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import tk.mybatis.mapper.entity.Condition;
import tk.mybatis.mapper.entity.Example;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;


/**
 * Created by zaq on 2018/07/10.
 */
@Service
@Transactional
public class PolicyOrderServiceImpl extends AbstractService<PolicyOrder> implements PolicyOrderService {
    @Resource
    private PolicyOrderMapper qyPolicyOrderMapper;

    @Resource
    private PolicyOrderService policyOrderService;

    @Resource
    private PolicyMapper policyMapper;

    @Resource
    private UnitMapper unitMapper;

    @Resource
    private ChargeMapper chargeMapper;

    @Resource
    private CommentMapper commentMapper;

    @Override
    public Integer selectVisibleState(Integer user_id, Integer goods_id, Integer goods_type) {
        Condition condition = new Condition(PolicyOrder.class);
        Example.Criteria criteria = condition.createCriteria();
        criteria.andCondition("user_id =" + user_id);
        if (goods_type == 1) {
            criteria.andCondition("policy_id =" + goods_id);
        } else if (goods_type == 2) {
            criteria.andCondition("unit_id =" + goods_id);
        } else if (goods_type == 3) {
            criteria.andCondition("charge_id =" + goods_id);
        }
        List<PolicyOrder> policyOrderList = policyOrderService.findByCondition(condition);
        Integer visibleState;
        if (policyOrderList.size() == 0) {
            visibleState = 1;//不可见
        } else {
            visibleState = 2;//可见
        }
        return visibleState;
    }

    @Override
    public List<Map> selectPolicyOrderList(PolicyOrder policyOrder) {
        return qyPolicyOrderMapper.selectPolicyOrderList(policyOrder);
    }

    @Override
    public List<Map> seePolicyOrder(Integer id) {
        List<Map> list = qyPolicyOrderMapper.seePolicyOrder(id);
        if (list.get(0).get("buy_type").equals(1)) {
            Map map = policyMapper.seePolicyOrder((Integer) list.get(0).get("policy_id"));
            list.get(0).put("policy", map);
        } else if (list.get(0).get("buy_type").equals(2)) {
            Map map = unitMapper.seePolicyOrder((Integer) list.get(0).get("unit_id"));
            list.get(0).put("unit", map);
        } else if (list.get(0).get("buy_type").equals(3)) {
            Map map = chargeMapper.seePolicyOrder((Integer) list.get(0).get("charge_id"));
            list.get(0).put("charge", map);
        }
        System.out.println(list.get(0).get("id"));
        List<Map> m = commentMapper.seePolicyOrder(id);
        list.get(0).put("comment", m);
        return list;
    }

    @Override
    public void getPhone(Integer id) {
        qyPolicyOrderMapper.getPhone(id);
    }

    @Override
    public List<PolicyOrder> selectPolicyOrderListByUserId(Member member) {
        return qyPolicyOrderMapper.selectPolicyOrderListByUserId(member);
    }

    @Override
    public List<Map> profit(Integer id, Integer buy_type) {
        return qyPolicyOrderMapper.profit(id, buy_type);
    }
}
